package org.leaf.green.b2b

import org.springframework.transaction.annotation.*
import org.hibernate.FetchMode as FM

class PurchaseAjaxService {

    static transactional = true
    //get staff list
    @Transactional(readOnly = true)
    def staffInstanceList(term) {
        log.info("staffInstanceList")
        def staffInstanceList = Staff.withCriteria{
            projections{
                property('id')
                property('serialNumber')
                property('name')
            }
            or{
                ilike("serialNumber", term + "%")
                ilike("name", "%"+term + "%")
            }
            maxResults(10)
            order("serialNumber", "asc")
        }

        return staffInstanceList
    }

    //get dealer list, recordType equal "供应商"
    @Transactional(readOnly = true)
    def dealerInstanceList(term) {
        log.info("dealerInstanceList")
        def dealerInstanceList = Dealer.withCriteria{

            createAlias 'recordType', 'rt'

            projections{
                property('id')
                property('serialNumber')
                property('alias')
            }

            fetchMode "recordType", FM.JOIN
            
            eq("rt.domain", "dealer")
            eq("rt.serialNumber", "1")
            or{
                ilike("serialNumber", term + "%")
                ilike("name", "%"+term + "%")
                ilike("alias", "%"+term + "%")
            }

            maxResults(10)

            order("serialNumber", "asc")
        }

        return dealerInstanceList
    }    
}
